package com.example.javaserve.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.example.javaserve.entity.UserBaseInfo;
import com.example.javaserve.mapper.UserBaseInfoMapper;


import com.example.javaserve.result.Result;
import com.example.javaserve.result.impl.ErrorResult;
import com.example.javaserve.result.impl.SuccessResult;
import com.example.javaserve.service.UserCRUD;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * @projectName: my_project
 * @package: com.example.javaserve.mapper.Impl
 * @className: UserCRUDImpl
 * @author: xxXPCSC
 * @description: TODO
 * @date: 2023/11/15 16:21
 * @version: 1.0
 **/
@Slf4j
@Service
@RequiredArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class UserCRUDImpl implements UserCRUD {
    private final UserBaseInfoMapper sysUserMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result create(UserBaseInfo user) {
        return sysUserMapper.insert(user) > 0 ? new SuccessResult("用户创建成功") : new ErrorResult("用户创建失败");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<UserBaseInfo> delete(UserBaseInfo user) {
        QueryWrapper<UserBaseInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(UserBaseInfo::getInfoUsername, user.getInfoUsername());
        return sysUserMapper.delete(queryWrapper) > 0 ? new SuccessResult<UserBaseInfo>() : new ErrorResult<UserBaseInfo>();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<UserBaseInfo> update(UserBaseInfo user) {
        return sysUserMapper.updateById(user) > 0 ? new SuccessResult<UserBaseInfo>() : new ErrorResult<UserBaseInfo>();
    }

    @Override
    public UserBaseInfo findByUserName(String userName) {
        return sysUserMapper.selectOne(new QueryWrapper<UserBaseInfo>().lambda().eq(UserBaseInfo::getInfoUsername, userName));
    }

    @Override
    public boolean registerEmailExist(String email) {
        QueryWrapper<UserBaseInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(UserBaseInfo::getInfoEmail, email);
        return sysUserMapper.selectOne(queryWrapper) != null;
    }

    @Override
    public Result passwordIsRight(String username,String password) {
        QueryWrapper<UserBaseInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(UserBaseInfo::getInfoUsername, username);
        UserBaseInfo userBaseInfo = sysUserMapper.selectOne(queryWrapper);
        System.out.println("数据库中"+password);
        System.out.println("传过来的"+userBaseInfo.getInfoPassword());
        if(userBaseInfo==null)
            return new ErrorResult("用户不存在");
        else if(userBaseInfo.getInfoPassword()!=password)
            return new ErrorResult("密码错误");
        return new SuccessResult("登录成功");
    }
}
